﻿using Domain.IRepositorys;
using Domain.Model;
using Domain.Services;
using Domain.Types;
using DTO;
using Infrastructure.Error;
using Infrastructure.UniOfWorkBase;
using Microsoft.Extensions.Logging;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Application.App
{
    public class UserPropApplication
    {
        public IUserPropBuyLogRepository userPropBuyLogRepository { get; set; }
        public IUserPropUseLogRepository userPropUseLogRepository { get; set; }
        public IUserRepository userRepository { get; set; }
        public ITaskRepository taskRepository { get; set; }
        public IWalletRepository walletRepository { get; set; }
        public UserPropService userPropService { get; set; }
        public ILogger<UserPropApplication> Logger { get; set; }
        public IEFUnitOfWork efUnitOfWork { get; set; }
        public FeeService feeService { get; set; }
        public TaskService taskService { get; set; }
        public WalletService walletService { get; set; }
        public IList<UserProp> GetUserProp(int uid)
        {
            var u = userRepository.GetByKey(uid);

            return u.UserProps;
        }

        public SXListOutput GetPropList(int uid, PropType propType)
        {
            var u = userRepository.GetByKey(uid);

            if (propType == PropType.刷新)
            {
                SXListOutput sXListOutput = new SXListOutput();

                var items = sXListOutput.Items = new List<SXListOutput.SxInfo>();

                items.Add(new SXListOutput.SxInfo() { Num = 1200, SrcFee = 360000, ZKFee = 90000 });
                items.Add(new SXListOutput.SxInfo() { Num = 600, SrcFee = 360000, ZKFee = 48000 });
                items.Add(new SXListOutput.SxInfo() { Num = 300, SrcFee = 360000, ZKFee = 27000 });
                items.Add(new SXListOutput.SxInfo() { Num = 100, SrcFee = 360000, ZKFee = 9500 });
                items.Add(new SXListOutput.SxInfo() { Num = 50, SrcFee = 360000, ZKFee = 5000 });
                items.Add(new SXListOutput.SxInfo() { Num = 20, SrcFee = 360000, ZKFee = 2500 });
                items.Add(new SXListOutput.SxInfo() { Num = 10, SrcFee = 360000, ZKFee = 1500 });

                return sXListOutput;
            }

            return null;
        }

        public int PayUserProp(int uid, PropType propType, int num)
        {
            var list = GetPropList(uid, propType);

            var buyItem = list.Items.FirstOrDefault(i => i.Num == num);

            if (buyItem == null)
            {
                throw new BizException($"没有这个规格的{propType.ToString()}道具");
            }
            var wallet = walletRepository.Entities.FirstOrDefault(i => i.UserID == uid);

            var chazhi = wallet.ChargeNeedPay(buyItem.ZKFee);

            if (chazhi > 0) return chazhi;

            feeService.PayUserPropFee(uid, propType, buyItem.Num, buyItem.ZKFee);

            userPropService.AddProp(uid, propType, buyItem.Num, buyItem.SrcFee, buyItem.ZKFee, buyItem.DCFee);

            efUnitOfWork.Commit();

            return 0;
        }

        public bool UseUserPropForTask(int uid, PropType propType, int taskID)
        {
            var u = userRepository.GetByKey(uid);

            var p = u.UserProps.FirstOrDefault(i => i.PropType == propType);

            if (p == null)
            {
                throw new BizException($"用户没有{propType.ToString()}道具");
            }
            if (p.RemainCount <= 0)
            {
                throw new BizException($"用户{propType.ToString()}道具已用完");
            }

            taskService.FreshTask(uid, taskID);
            p.RemainCount--;

            UserPropUseLogAggregate userPropUseLogAggregate = new UserPropUseLogAggregate();
            userPropUseLogAggregate.BizID = taskID.ToString();
            userPropUseLogAggregate.CT = userPropUseLogAggregate.UT = DateTime.Now;
            userPropUseLogAggregate.PropID = p.ID.ToString();
            userPropUseLogAggregate.PropType = p.PropType;
            userPropUseLogAggregate.UserID = u.ID;
            userPropUseLogRepository.Insert(userPropUseLogAggregate);

            return efUnitOfWork.Commit() > 0;
        }

        public RecommendPropInfoOutput GetRecommendPropInfo(int uid)
        {
            RecommendPropInfoOutput recommendPropInfoOutput = new RecommendPropInfoOutput();
            var u = userRepository.GetByKey(uid);

            //var time = u.GetTraderLevelConfig<int>("任务上推荐费用[小时]");
            var fee = u.GetTraderLevelConfig<int>("任务上推荐费用");
            var pre = u.GetTraderLevelConfig<int>("任务上推荐减免比例");

            //recommendPropInfoOutput.Hour = time;
            recommendPropInfoOutput.SrcFee = fee;
            recommendPropInfoOutput.ZKRate = pre;
            recommendPropInfoOutput.ZKFee = fee * pre / 100;

            return recommendPropInfoOutput;
        }

        public int PayRecommendPropForTask(int uid, int hour, int taskID)
        {
            if (taskService.HasRecommend(taskID))
            {
                throw new BizException("该任务已经在推荐列表中！");
            }
            var u = userRepository.GetByKey(uid);

            var fee = u.GetTraderLevelConfig<int>("任务上推荐费用");
            var pre = u.GetTraderLevelConfig<int>("任务上推荐减免比例");

            var totalFee = fee * pre * hour;

            var wallet = walletRepository.Entities.FirstOrDefault(i => i.UserID == uid);

            var chazhi = wallet.ChargeNeedPay(totalFee);

            if (chazhi > 0) return chazhi;

            taskService.PutRecommendListForHour(uid, taskID, hour, true, "用户支付上推荐");

            walletService.ChangeChargeWallet(uid, -totalFee, totalFee, taskID.ToString(), FinancialBizType.任务上推荐, "任务上推荐", "任务上推荐");
            efUnitOfWork.Commit();

            return 0;
        }
    }
}
